﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SPA_Prototype.Model
{
    class PromotionManagement
    {
        private int _VIPRoomCharge;
        private int _VIPDiscount;

        private DatabaseDataSet _Dataset;
        private DatabaseDataSet.PromotionTableDataTable _DataTable;
        private DatabaseDataSetTableAdapters.PromotionTableTableAdapter _Adt;

        // Constructor
        public PromotionManagement(DatabaseDataSet dt)
        {
            _Dataset = dt;
            _DataTable = _Dataset.PromotionTable;
            _Adt = new DatabaseDataSetTableAdapters.PromotionTableTableAdapter();

            LoadDB();
        }
        // Property 
        public int VIPRoomCharge
        {
            get { return _VIPRoomCharge; }
            set
            {
                if (value <= 0 || value == _VIPRoomCharge) return;                
                _VIPRoomCharge = value;
                if (_DataTable.Rows.Count == 0) _DataTable.AddPromotionTableRow(1, 1);
                _DataTable[0].VIPRoomCharge = _VIPRoomCharge;
                UpdateDB();
            }
        }

        public int VIPDiscount
        {
            get { return _VIPDiscount; }
            set
            {
                if (value <= 0 || value == _VIPDiscount) return;
                _VIPDiscount = value;
                if (_DataTable.Rows.Count == 0) _DataTable.AddPromotionTableRow(1, 1);
                _DataTable[0].VIPDiscount = _VIPDiscount;
                UpdateDB();
            }

        }
        // Method
        private void LoadDB()
        {
            _Adt.Fill(_DataTable);
            if (_DataTable.Rows.Count != 0)
            {
                _VIPDiscount = _DataTable[0].VIPDiscount;
                _VIPRoomCharge = _DataTable[0].VIPRoomCharge;
            }
            else
            {
                _VIPDiscount = 0;
                _VIPRoomCharge = 0;
            }
        }

        private void UpdateDB()
        {
            try
            {
               _Adt.Update(_DataTable);
               MessageBox.Show("Successful !", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
